package com.maven.front.util;

import com.maven.front.bean.EduCourse;
import com.maven.front.mapper.EduCourseMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @descriptions:  定时器, 用来改变课程状态
 * @author:
 * @date: 2020/8/25 15:13
 * @version: 1.0
 */

@Component
@Slf4j
public class Timer {

    @Resource
    private EduCourseMapper eduCourseMapper;

    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Scheduled(cron = "0 0 0 * * ?")//设置每天23.59执行
//    @Scheduled(fixedRate = 5000)  //测试，每隔5秒执行一次
    public void timerTest() throws Exception{
        EduCourse eduCourse = new EduCourse();
        //获取当前时间
        Date date = new Date();
        //获取所有课程
        List<EduCourse> list = eduCourseMapper.select(null);
        for (EduCourse course : list){//循环获取单个课程\
//            Date endTime = sdf.format(course.getEndTime());
            if (course.getEndTime().getTime() <= date.getTime()){
                if (course.getIsAvaliable()==1){//判断当前课程是否为上架状态
                    eduCourse.setCourseId(course.getCourseId());//要修改的课程的ID
                    eduCourse.setIsAvaliable(2);//将结束时间大于或等于当前时间的课程下架
                    eduCourseMapper.updateByPrimaryKeySelective(eduCourse);//修改课程状态
                }
            }
        }
    }

}
